/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.acionista.view;

import br.jm.acionista.entity.CepUf;
import br.jm.acionista.entity.CodigoDdd;
import br.jm.acionista.sistema.eao.CepUfEao;
import br.jm.acionista.sistema.eao.CodigoDddEao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;


/**
 *
 * @author joao
 */
public class CodigoDddBean implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private CodigoDdd selectedCodigoDdd;
    private LazyDataModel<CodigoDdd> lazyModel;
    private CodigoDddEao codigoDddJpa;
    private List<CepUf> cepUfList;
    private CepUfEao cepUfJpa;
    private int nTotalReg;
    private Map<String, String> pesquisas = new HashMap<String, String>();
    private boolean disable;

    public CodigoDddBean() {
        codigoDddJpa = CodigoDddEao.getInstance();
        selectedCodigoDdd = new CodigoDdd();

        this.disable = true;
        
        this.lazyModel = new LazyDataModel<CodigoDdd>() {
            public List<CodigoDdd> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters) {
                
                if (filters.isEmpty()) {
                    filters.put("municipio", "sao paulo");
                }
                
                List<CodigoDdd> lazyCodigoDdd = new ArrayList<CodigoDdd>();

                try {
                    lazyCodigoDdd = codigoDddJpa.getNative("CodigoDdd", first, pageSize, sortField, sortOrder, filters, null);
                    this.setPageSize(pageSize);
                } catch (Exception ex) {
                    Logger.getLogger(CodigoDddBean.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (lazyCodigoDdd.isEmpty()) {
                    selectedCodigoDdd = new CodigoDdd();
                    selectedCodigoDdd.setLocalidade("NENHUM REGISTRO RETORNADO ...");
                    lazyCodigoDdd.add(selectedCodigoDdd);
                }
                return lazyCodigoDdd;
            }

        };

        nTotalReg = codigoDddJpa.getCount("CodigoDdd");
        if (nTotalReg == 0) {
            nTotalReg = 1;
            selectedCodigoDdd = new CodigoDdd();
        }
        lazyModel.setRowCount(nTotalReg);
    }

    public void inserir(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            selectedCodigoDdd.setIdDdd(null);
            codigoDddJpa.create(selectedCodigoDdd);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Código D.D.D. Inserido com Sucesso",
                    selectedCodigoDdd.getLocalidade());

        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Inserir" + selectedCodigoDdd.getLocalidade(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void alterar(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            codigoDddJpa.edit(selectedCodigoDdd);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Código D.D.D. Alterado com Sucesso",
                    selectedCodigoDdd.getLocalidade());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Alterar: " + selectedCodigoDdd.getLocalidade(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void excluir(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            codigoDddJpa.destroy(selectedCodigoDdd);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Código D.D.D. Excluido com Sucesso",
                    selectedCodigoDdd.getLocalidade());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Excluir: " + selectedCodigoDdd.getLocalidade(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public LazyDataModel<CodigoDdd> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<CodigoDdd> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public CodigoDdd getSelectedCodigoDdd() {
        /*
        if (selectedCodigoDdd == null) {
            selectedCodigoDdd = new CodigoDdd();
        }
        * 
        */
        return selectedCodigoDdd;
    }

    public void setSelectedCodigoDdd(CodigoDdd selectedCodigoDdd) {
        this.selectedCodigoDdd = selectedCodigoDdd;
    }
    
    public List<String> complete(String query) {
        pesquisas.clear();
        pesquisas.put("codUf", query);
        List<String> results = new ArrayList<String>();

        cepUfList = new ArrayList<CepUf>();
        try {
            cepUfList = cepUfJpa.getNative("CepUf", 0, 15, "dscEstado", true, pesquisas, null);

            for (int i = 0; i < cepUfList.size(); i++) {
                results.add(cepUfList.get(i).getCodUf());
            }
        } catch (Exception ex) {
            Logger.getLogger(CidadeBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return results;
    }
    
    public void onRowSelectDDD(SelectEvent event) {
        this.disable = true;
    }

    public boolean isDisable() {
        return disable;
    }

    public void setDisable(boolean disable) {
        this.disable = disable;
    }
    
    public CodigoDdd novo() {
        this.disable = false;
        this.selectedCodigoDdd = new CodigoDdd();
        
        return this.selectedCodigoDdd;
    }
}
